import pagesJson from '@/pages.json';
import { translate as t } from '@/locale/index';

console.log(pagesJson);

/** 判断当前页面是否是tabbar页  */
export const getIsTabbar = () => {
	if (!Object.keys(pagesJson).includes('tabBar')) {
		return false;
	}
	const pages = getCurrentPages();
	const currPath = pages.at(-1).route;
	return !!pagesJson.tabBar.list.find(e => e.pagePath === currPath);
};

/**
 * test i18n in not .vue file
 */
export const testI18n = () => {
	console.log(t('app.name'));
	// 下面同样生效
	uni.showModal({
		title: 'i18n 测试',
		content: t('app.name'),
	});
};

/**
 * 获取计划状态的文字描述
 */
export const getPlanStatusToTex = (val: number) => {
	switch (val) {
		case 0:
			return '未开始';
		case 1:
			return '进行中';
		case 2:
			return '暂停';
		default:
			return '已结束';
	}
};
/**
 * 获取年级文字描述
 */
export const getGradToTex = (val: string | number) => {
	switch (val) {
		case '1':
			return '一年级';
		case '2':
			return '二年级';
		case '4':
			return '三年级';
		case '8':
			return '四年级';
		case '16':
			return '五年级';
		case '32':
			return '六年级';
		case '64':
			return '初一';
		case '128':
			return '初二';
		case '256':
			return '初三';
		default:
			return '未知';
	}
};

export const debounce = (fn, time) => {
	let timer = null;
	return (...args) => {
		const _this = this;
		const context = _this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(() => {
			fn.call(context, ...args);
		}, time);
	};
};

/**
 * @param {Object} n：数据转化
 */
export const formatNumber = n => {
	const str = n.toString();

	return str[1] ? str : `0${str}`;
};

/**
 * 时间戳转化为年 月 日 时 分 秒
 * number: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
export const formatTime = (number: number, format: string, precision: boolean = true) => {
	let str = format;
	const formateArrE = ['Y', 'M', 'D', 'h', 'm', 's'];
	const formateArrZ = ['年', '月', '日', '时', '分', '秒'];
	let filed = 'en';

	let arr = formateArrE;
	if (formateArrZ.find(item => str.indexOf(item) !== -1)) {
		arr = formateArrZ;
		filed = 'zh';
	}
	const returnArr = [];

	const date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(precision ? formatNumber(date.getMonth() + 1) : date.getMonth() + 1);
	returnArr.push(precision ? formatNumber(date.getDate()) : date.getDate());

	returnArr.push(precision ? formatNumber(date.getHours()) : date.getHours());
	returnArr.push(precision ? formatNumber(date.getMinutes()) : date.getMinutes());
	returnArr.push(precision ? formatNumber(date.getSeconds()) : date.getSeconds());

	Object.keys(returnArr).forEach(key => {
		str = str.replace(arr[key], filed === 'en' ? returnArr[key] : returnArr[key] + arr[key]);
	});

	return str;
};

export const initializeApp = async (...promises: Promise<void>[]) => {
	uni.showLoading({ title: '加载中...' });
	try {
		await Promise.all(promises);
	} catch (e) {
		console.error(e);
	} finally {
		uni.hideLoading();
	}
};

// 计算两个时间戳之间相差的天数
export const calculateDaysBetweenTimestamps = (timestamp1: number, timestamp2: number) => {
	const SECONDS_IN_DAY = 86400; // 一天的秒数

	// 计算两个时间戳之差，得到秒数差
	const differenceInSeconds = Math.abs(timestamp1 - timestamp2);

	// 将秒数差转换为天数
	const daysDifference = differenceInSeconds / SECONDS_IN_DAY;

	// 返回相差的天数，通常保留整数部分即可
	return Math.floor(daysDifference);
};
export const getDurationName = (num: string) => {
	switch (num) {
		case '1':
			return '无限期';
		case '2':
			return '7天';
		case '3':
			return '8天';
		case '4':
			return '9天';
		case '5':
			return '15天';
		case '6':
			return '30';
		case '7':
			return '60';
		default:
			return '未知';
	}
};

export const getPlanDurationId = (num: number) => {
	switch (num) {
		case 7:
			return '2';
		case 8:
			return '3';
		case 9:
			return '4';
		case 15:
			return '5';
		case 30:
			return '6';
		case 60:
			return '7';
		default:
			return '1';
	}
};
